<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '规格添加' : '规格修改'"
      :visible.sync="info.isshow"
      @close="close"
    >
      {{ user }}{{attrArr}}
      <el-form :model="user">
        <el-form-item label="规格名称" label-width="120px">
          <el-input v-model="user.specsname" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item
          label="规格属性"
          label-width="120px"
          v-for="(item, index) in attrArr"
          :key="index"
        >
          <div class="inputAndBtn">
            <el-input v-model="item.title" autocomplete="off"></el-input>
            <el-button
              type="primary"
              v-if="index == 0"
              @click="attrArr.push({ title: '' })"
              >新增规格属性</el-button
            >
            <el-button type="danger" v-else @click="attrArr.splice(index, 1)"
              >删除</el-button
            >
          </div>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <!-- </el-form> -->

      <div slot="footer" class="dialog-footer">
        <el-button>取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add"
          >添 加</el-button
        >
        <el-button type="primary" v-else @click="update">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import path from "path";
import { successAlert, errorAlert } from "../../../utils";
import { reqspecsadd, reqspecsedit } from "../../../http/api";

export default {
  props: ["info"],
  computed: {
    // attrs() {
    //   return this.attrArr.reduce(
    //     (val, item) => val + "," + item.title,
    //     this.str
    //   );
    // },
  },
  mounted() {},
  methods: {
    addSpace() {
      this.attrArr.push({
        title: "",
        id: Math.random() * 1000 + new Date().getTime(),
      });
    },
    close(){
      if(!this.info.isAdd){
        this.empty()
      }
    },
    getOne(id) {
      reqspecsinfo({ id }).then((res) => {
        // console.log('一条',res);
        this.user = res.data.list[0];
        this.info.isshow = true;
        this.info.isAdd = false;
        this.attrArr = this.user.attrs.map(item => ({title:item}))
      });
    },
    add() {
      this.user.attrs = this.attrArr.map((item) => item.title).join(",");
      reqspecsadd(this.user).then((res) => {
        successAlert(res.data.msg);
        this.info.isshow = false;
        this.empty();
        this.$emit("init");
      });
      // console.log(this.user);
    },
    empty() {
      this.user = {
        specsname: "",
        attrs: "",
        status: 1,
      },
        this.attrArr = [
        {
          title: "",
        },
        {
          title: "",
        },
      ];
    },
    update(){
      this.user.attrs = this.attrArr.map((item) => item.title).join(",");

      // console.log(this.user);
      reqspecsedit(this.user).then(res => {
        
        successAlert(res.data.msg);
        this.empty()
        this.info.isshow = false;
        this.$emit('init')
        console.log(this.user);
      })
    },
    close(){
      if(!this.info.isAdd){
        this.empty()
      }
    }
  },

  data() {
    return {
      user: {
        specsname: "",
        attrs: "",
        status: 1,
      },
      attrArr: [
        {
          title: "",
        },
        {
          title: "",
        },
      ],
    };
  },
};
</script>

<style scoped lang="less">
.inputAndBtn {
  display: flex;
  .el-button {
    width: auto;
  }
  .el-input {
    flex: 1;
  }
}
</style>